package test;

import java.util.Stack;

public class Solution {

    public boolean validateStackSequences(int[] pushed, int[] popped) {
        if (popped.length != pushed.length) {
            return false;
        }
        // 在此处写入代码
        Stack<Integer> stack = new Stack<Integer>();
        int popIndex = 0;
        for (int i = 0; i < popped.length; ) {

            if (stack.size() > 0 && stack.get(stack.size() - 1) == popped[popIndex]) {

                stack.pop();
                popIndex ++;
            }
            if (popped[popIndex] != pushed[i]) {
                stack.push(pushed[i]);

            } else {
                popIndex ++ ;
            }
            i++;


        }
        if (stack.size() == 0) {
            return true;
        }
        while (stack.size() > 0) {
            int item = stack.pop();
            if (item != popped[popIndex]) {
                return false;
            }
            popIndex ++ ;

        }
        return true;


    }
}
